package com.xiaoer.studyenglishdemo.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.xiaoer.studyenglishdemo.R;
import com.xiaoer.studyenglishdemo.bean.WordBean;

import java.util.ArrayList;
import java.util.List;

public class WordsTrainActivity extends AppCompatActivity {

    ImageView iv_back;
    ImageView iv_img;
    LinearLayout ll_btn1;
    TextView tv_introduce;
    LinearLayout ll_btn2;
    EditText edt_means;
    LinearLayout ll_btn3;
    EditText edt_short_sentence;
    LinearLayout ll_btn4;
    EditText edt_sentence1;
    LinearLayout ll_btn5;
    EditText edt_sentence2;
    LinearLayout ll_btn6;
    EditText edt_sentence3;
    TextView tv_page;
    Button btn_last;
    Button btn_next;

    TextView tv_edt_means;
    TextView tv_edt_short_sentence;
    TextView tv_edt_sentence1;
    TextView tv_edt_sentence2;
    TextView tv_edt_sentence3;
    Button btn_check;

    TextView tv_means_tip;
    TextView tv_short_sentence_tip;
    TextView tv_sentence1_tip;
    TextView tv_sentence2_tip;
    TextView tv_sentence3_tip;
    private int page = 0;

    private List<WordBean> wordBeans = new ArrayList<>();

    private boolean isTrain = true;
    private WordBean curWord = null;


    int scope = 0;

    public static void startMe(Context context, ArrayList<WordBean> wordBeans, boolean isTrain) {
        Intent intent = new Intent(context, WordsTrainActivity.class);
        intent.putExtra("words", wordBeans);
        intent.putExtra("isTrain", isTrain);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        wordBeans.addAll(getIntent().getParcelableArrayListExtra("words"));
        isTrain = getIntent().getBooleanExtra("isTrain", true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_train);

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        iv_img = findViewById(R.id.iv_img);
        ll_btn1 = findViewById(R.id.ll_btn1);
        tv_introduce = findViewById(R.id.tv_introduce);
        ll_btn2 = findViewById(R.id.ll_btn2);
        edt_means = findViewById(R.id.edt_means);
        ll_btn3 = findViewById(R.id.ll_btn3);
        edt_short_sentence = findViewById(R.id.edt_short_sentence);
        ll_btn4 = findViewById(R.id.ll_btn4);
        edt_sentence1 = findViewById(R.id.edt_sentence1);
        ll_btn5 = findViewById(R.id.ll_btn5);
        edt_sentence2 = findViewById(R.id.edt_sentence2);
        ll_btn6 = findViewById(R.id.ll_btn6);
        edt_sentence3 = findViewById(R.id.edt_sentence3);
        tv_page = findViewById(R.id.tv_page);
        btn_last = findViewById(R.id.btn_last);
        btn_next = findViewById(R.id.btn_next);
        tv_edt_means = findViewById(R.id.tv_edt_means);
        tv_edt_short_sentence = findViewById(R.id.tv_edt_short_sentence);
        tv_edt_sentence1 = findViewById(R.id.tv_edt_sentence1);
        tv_edt_sentence2 = findViewById(R.id.tv_edt_sentence2);
        tv_edt_sentence3 = findViewById(R.id.tv_edt_sentence3);
        btn_check = findViewById(R.id.btn_check);
        tv_means_tip = findViewById(R.id.tv_means_tip);
        tv_short_sentence_tip = findViewById(R.id.tv_short_sentence_tip);
        tv_sentence1_tip = findViewById(R.id.tv_sentence1_tip);
        tv_sentence2_tip = findViewById(R.id.tv_sentence2_tip);
        tv_sentence3_tip = findViewById(R.id.tv_sentence3_tip);

        if (!isTrain) {
            btn_next.setTextSize(16);
        }

        updatePage();

        btn_next.setOnClickListener(v -> {
            if (isTrain) {
                if (page < wordBeans.size()) {
                    page++;
                }
                updatePage();
            } else {
                btn_check.performClick();
                if (page < wordBeans.size()) {
                    page++;
                }
                updatePage();
            }

        });
        btn_last.setOnClickListener(v -> {
            if (page != 0) {
                page--;
                updatePage();
            } else {
                Toast.makeText(this, "到头啦~", Toast.LENGTH_SHORT).show();
            }
        });
        btn_check.setOnClickListener(v -> {
            if (curWord == null) {
                return;
            }
            if (isTrain) {
                tv_edt_means.setVisibility(View.VISIBLE);
                tv_edt_short_sentence.setVisibility(View.VISIBLE);
                tv_edt_sentence1.setVisibility(View.VISIBLE);
                tv_edt_sentence2.setVisibility(View.VISIBLE);
                tv_edt_sentence3.setVisibility(View.VISIBLE);
            }

            boolean isAddScope = true;
            if (TextUtils.equals(edt_means.getText().toString(), curWord.getMeans())) {
                if (isTrain) {
                    tv_edt_means.setText("正确");
                    tv_edt_means.setTextColor(Color.GREEN);
                    tv_means_tip.setVisibility(View.GONE);
                }
            } else {
                if (isTrain) {
                    tv_edt_means.setText("错误");
                    tv_edt_means.setTextColor(Color.RED);
                    tv_means_tip.setText(curWord.getMeans());
                    tv_means_tip.setVisibility(View.VISIBLE);
                }
                isAddScope = false;
            }
            if (TextUtils.equals(edt_short_sentence.getText().toString(), curWord.getShortSentence())) {
                if (isTrain) {
                    tv_edt_short_sentence.setText("正确");
                    tv_edt_short_sentence.setTextColor(Color.GREEN);
                    tv_short_sentence_tip.setVisibility(View.GONE);
                }
            } else {
                if (isTrain) {
                    tv_edt_short_sentence.setText("错误");
                    tv_edt_short_sentence.setTextColor(Color.RED);
                    tv_short_sentence_tip.setText(curWord.getShortSentence());
                    tv_short_sentence_tip.setVisibility(View.VISIBLE);
                }
                isAddScope = false;
            }
            if (TextUtils.equals(edt_sentence1.getText().toString(), curWord.getSentence1())) {
                if (isTrain) {
                    tv_edt_sentence1.setText("正确");
                    tv_edt_sentence1.setTextColor(Color.GREEN);
                    tv_sentence1_tip.setVisibility(View.GONE);
                }
            } else {
                if (isTrain) {
                    tv_edt_sentence1.setText("错误");
                    tv_edt_sentence1.setTextColor(Color.RED);
                    tv_sentence1_tip.setText(curWord.getSentence1());
                    tv_sentence1_tip.setVisibility(View.VISIBLE);
                }
                isAddScope = false;
            }
            if (TextUtils.equals(edt_sentence2.getText().toString(), curWord.getSentence2())) {
                if (isTrain) {
                    tv_edt_sentence2.setText("正确");
                    tv_edt_sentence2.setTextColor(Color.GREEN);
                    tv_sentence2_tip.setVisibility(View.GONE);
                }
            } else {
                if (isTrain) {
                    tv_edt_sentence2.setText("错误");
                    tv_edt_sentence2.setTextColor(Color.RED);
                    tv_sentence2_tip.setText(curWord.getSentence2());
                    tv_sentence2_tip.setVisibility(View.VISIBLE);
                }
                isAddScope = false;
            }
            if (TextUtils.equals(edt_sentence3.getText().toString(), curWord.getSentence3())) {
                if (isTrain) {
                    tv_edt_sentence3.setText("正确");
                    tv_edt_sentence3.setTextColor(Color.GREEN);
                    tv_sentence3_tip.setVisibility(View.GONE);
                }
            } else {
                if (isTrain) {
                    tv_edt_sentence3.setText("错误");
                    tv_edt_sentence3.setTextColor(Color.RED);
                    tv_sentence3_tip.setText(curWord.getSentence3());
                    tv_sentence3_tip.setVisibility(View.VISIBLE);
                }
                isAddScope = false;
            }

            if (isAddScope) {
                scope += 10;
            }
        });

        if (isTrain) {
            btn_last.setVisibility(View.VISIBLE);
            btn_check.setVisibility(View.VISIBLE);
        } else {
            btn_last.setVisibility(View.GONE);
            btn_check.setVisibility(View.GONE);
        }
    }

    public void updatePage() {
        if (page >= wordBeans.size()) {
            if (isTrain) {
                Toast.makeText(this, "完成~", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                new AlertDialog.Builder(WordsTrainActivity.this)
                        .setCancelable(false)
                        .setMessage("您的分数为" + scope + "分")
                        .setPositiveButton("知道啦", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                finish();
                            }
                        }).show();
            }
        } else {
            btn_next.setText(page >= wordBeans.size() - 1 ? "完成" : "下一页");
            curWord = wordBeans.get(page);
            if (!TextUtils.isEmpty(curWord.getImgUrl())) {
                if (curWord.getImgUrl().contains("content://")) {
                    Glide.with(this).load(Uri.parse(curWord.getImgUrl())).into(iv_img);
                } else {
                    Glide.with(this).load(curWord.getImgUrl()).into(iv_img);
                }
            } else {
                Glide.with(this).load(curWord.getImgLocal()).into(iv_img);
            }
            tv_introduce.setText(curWord.getIntroduce());
            edt_means.setText(curWord.getMeans());
            edt_short_sentence.setText(curWord.getShortSentence());
            edt_sentence1.setText(Html.fromHtml(curWord.getSentence1().replace(curWord.getIntroduce(), "<font color='#FF0000'>" + curWord.getIntroduce() + "</font>")));
            edt_sentence2.setText(Html.fromHtml(curWord.getSentence2().replace(curWord.getIntroduce(), "<font color='#FF0000'>" + curWord.getIntroduce() + "</font>")));
            edt_sentence3.setText(Html.fromHtml(curWord.getSentence3().replace(curWord.getIntroduce(), "<font color='#FF0000'>" + curWord.getIntroduce() + "</font>")));
            tv_page.setText(((page + 1)) + "/" + wordBeans.size());
            tv_edt_means.setVisibility(View.GONE);
            tv_edt_short_sentence.setVisibility(View.GONE);
            tv_edt_sentence1.setVisibility(View.GONE);
            tv_edt_sentence2.setVisibility(View.GONE);
            tv_edt_sentence3.setVisibility(View.GONE);
            tv_means_tip.setVisibility(View.GONE);
            tv_short_sentence_tip.setVisibility(View.GONE);
            tv_sentence1_tip.setVisibility(View.GONE);
            tv_sentence2_tip.setVisibility(View.GONE);
            tv_sentence3_tip.setVisibility(View.GONE);
            if (isTrain) {
                randomHide();
            } else {
                randomHide();
            }
        }
    }

    public void randomHide() {
        // 创造一个0到3的随机数
        int random = (int) (Math.random() * 3);
        ll_btn2.setVisibility(View.VISIBLE);
        if (random == 0) {
            edt_short_sentence.setText("");
        }
        if (random == 1) {
            edt_sentence1.setText("");
        }
        if (random == 2) {
            edt_sentence2.setText("");
        }
        if (random == 3) {
            edt_sentence3.setText("");
        }
    }

    public void hideAll() {
        edt_means.setText("");
        ll_btn2.setVisibility(View.VISIBLE);
        edt_short_sentence.setText("");
        edt_sentence1.setText("");
        edt_sentence2.setText("");
        edt_sentence3.setText("");
    }
}